﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Newtonsoft.Json.Linq;
using System.IO;
using Newtonsoft.Json;
using System.Net.Cache;
using BuscoPre.Domain;

namespace BuscoPre.Repository
{
	public abstract class GenericRestMessageService<T> where T : class
	{
		private static readonly string RequestAccept = "application/json";
		private static readonly string RequestContentType = "application/json; charset=utf-8";

		/// <summary>
		/// Create web request for rest call.
		/// </summary>
		/// <param name="requestUri">The url.</param>
		/// <returns>A custom Json Array </returns>
		public object Get(string requestUri)
		{
			HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
			request.Method = WebRequestMethods.Http.Get;
			request.Accept = RequestAccept;
			request.ContentType = RequestContentType;

			// Get the response.
			WebResponse response = GetSpecificWebResponse(request);

			// Get the stream containing content returned by the server.
			Stream dataStream = response.GetResponseStream();
			// Open the stream using a StreamReader for easy access.
			StreamReader reader = new StreamReader(dataStream);
			// Read the content.
			string responseFromServer = reader.ReadToEnd();

			object result = GetSpecificResponse(request, responseFromServer);

			// Close stream.
			reader.Close();
			response.Close();

			return (result);
		}

		public abstract object GetSpecificResponse(HttpWebRequest request, string responseFromServer);

		public virtual WebResponse GetSpecificWebResponse(HttpWebRequest request)
		{
			return request.GetResponse();
		}
	}

	public class CategoriesRestMessageService : GenericRestMessageService<Category>
	{
		public override object GetSpecificResponse(HttpWebRequest request, string responseFromServer)
		{
			var json = responseFromServer.Remove(0, 2);
			json = json.Remove(json.Length - 2, 2);

			object result = (object)JsonConvert.DeserializeObject(json);

			return result;
		}

		public override WebResponse GetSpecificWebResponse(HttpWebRequest request)
		{
			request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);
			return base.GetSpecificWebResponse(request);
		}
	}

	public class HotItemsRestMessageService : GenericRestMessageService<Article>
	{
		public override object GetSpecificResponse(HttpWebRequest request, string responseFromServer)
		{
			object result = (object)JsonConvert.DeserializeObject(responseFromServer);

			return result;
		}

	}

	public class SearchItemRestMessageService : GenericRestMessageService<Article>
	{
		public override object GetSpecificResponse(HttpWebRequest request, string responseFromServer)
		{
			var result = JsonConvert.DeserializeObject(responseFromServer);

			return result;
		}

		public override WebResponse GetSpecificWebResponse(HttpWebRequest request)
		{
			request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.CacheIfAvailable);
			return base.GetSpecificWebResponse(request);
		}
	}


}
